﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Styx;
using Styx.Common;
using Styx.Helpers;
using Styx.CommonBot;

namespace BuddyManager
{
    public partial class TaskingPopup : Form
    {
        public bool Cancelled;
        public List<string> NonProfileBB = new List<string> { "BG Bot [Beta]", "PvP", "ArchaeologyBuddy", "Instancebuddy", "DungeonBuddy", "BGBuddy" };
        public ListViewItem Item;
        public bool AddingTask;
        public bool SwitchTime;
        public List<string> InternalSubList = new List<string>();

        public TaskingPopup(bool AddTask, ListViewItem item)
        {
            InitializeComponent();
            //Logging.Write("AFTER INITIALIZECOMPONENT");
            AddingTask = AddTask;
            if (AddTask) 
            { 
                Item = new ListViewItem();
                return;
            }
            Item = item;

        }

        public void TaskingPopup_Load(object sender, EventArgs args)
        {
            
            Cancelled = true;
            //Logging.Write("FIRST STEP OF CONFIG");
            foreach (KeyValuePair<string, BotBase> BB in BotManager.Instance.Bots)
            {
                if (!BB.Key.ToLower().Equals("pvp") && !BB.Key.ToLower().Equals("mixed mode") && !BB.Key.ToLower().Equals("combat bot") && !BB.Key.ToLower().Equals("lazyraider") && !BB.Key.ToLower().Equals("partybot") && !BB.Key.ToLower().Equals("questing") && !BB.Key.ToLower().Equals("raid bot"))
                {
                    if (!taskBotBase.Items.Contains(BB.Key))
                    {
                        taskBotBase.Items.Add(BB.Key);
                    }
                }
            }
            if (AddingTask)
            {
                this.Text = "BuddyManager Tasking - Add A Task";
                SwitchAfterTimeRadio_CheckedChanged(null, null);
                return;
            }
            this.Text = "BuddyManager Tasking - Edit A Task";
            
            //Logging.Write("AFTER FOREACH");
            taskBotBase.Text = Item.SubItems[1].Text;
            taskZone.Text = Item.SubItems[2].Text;
            if (Item.SubItems[4].Text == "Minutes")
            {
                SwitchAfterTimeRadio.Checked = true;
                SwitchAfterNodes.Checked = false;
                taskMinutes.Value = Item.SubItems[3].Text.ToInt32();
                SwitchTime = true;
            }
            else
            {
                SwitchTime = false;
                SwitchAfterNodes.Checked = true;
                SwitchAfterTimeRadio.Checked = false;
                NumberOfNodes.Value = Item.SubItems[3].Text.ToInt32();
            }
            SwitchAfterTimeRadio_CheckedChanged(null, null);
            taskBotBase_SelectedIndexChanged(null, null);
            var str = Item.SubItems[6].Text;
            if (str != "None" && !String.IsNullOrEmpty(str))
            {
                var strA = str.Split(new Char[] { ';' });
                foreach (var s in strA)
                { s.Replace(";", ""); InternalSubList.Add(s); }
            }
            foreach (var p in InternalSubList)
                listViewSub.Items.Add(p);
            SwitchAfterTimeRadio_CheckedChanged(null, null);
            taskBotBase_SelectedIndexChanged(null, null);
            EnableDisableSubProfiles();
            //Logging.Write("BEFORE FOREACH");
        }

        public void ModifyComponents()
        {
            if (NonProfileBB.Contains(taskBotBase.Text))
            {
                taskZone.Text = "None";
                taskProfile.Text = "None";
                taskZone.Enabled = false;
                taskProfile.Enabled = false;
                SwitchAfterTimeRadio.Checked = true;
                SwitchAfterNodes.Checked = false;
                SwitchAfterNodes.Enabled = false;
                EnableDisableSubProfiles();
            }
            else
            {
                taskProfile.Enabled = true;
                taskZone.Enabled = true;
                SwitchAfterNodes.Enabled = true;
                EnableDisableSubProfiles();
            }
            //Logging.Write("MODYCOMPS END");
        }

        private void taskProfile_DoubleClick(object sender, EventArgs e)
        {
            var SP1 = new OpenFileDialog
            {
                Filter = "Xml files | *.xml",
                Title = "Select xml file to load!"
            };

            if (SP1.ShowDialog() == DialogResult.OK)
            {
                string profile1 = SP1.FileName;
                if (!string.IsNullOrEmpty(profile1))
                {
                    taskProfile.Text = profile1;
                    //BuddyManagerSettings.Instance.SelectedProfile1 = taskProfile.Text;
                }
            }
        }

        private void taskBotBase_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModifyComponents();
            if (taskBotBase.Text == "Gatherbuddy2")
            {
                NumberOfNodes.Enabled = true;
                SwitchAfterNodes.Enabled = true;
                label8.Enabled = true;
            }
            else
            {
                NumberOfNodes.Enabled = false;
                SwitchAfterNodes.Enabled = false;
                label8.Enabled = false;
            }
        }

        private void EnableDisableSubProfiles()
        {
            if (taskBotBase.Text == "Gatherbuddy2" || taskBotBase.Text == "Grind Bot")
                groupBoxSub.Enabled = true;
            else
            {
                if (InternalSubList.Count > 0)
                    buttonAddProfile.Enabled = false;
                else
                    buttonAddProfile.Enabled = true;
            }
            this.Update();
            this.Refresh();

        }

        private void taskZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModifyComponents();
        }

        private void taskHours_ValueChanged(object sender, EventArgs e)
        {
            ModifyComponents();
        }

        private void taskMinutes_ValueChanged(object sender, EventArgs e)
        {
            ModifyComponents();
        }

        private void taskProfile_TextChanged(object sender, EventArgs e)
        {
            ModifyComponents();
        }

        private void SaveClose_Click(object sender, EventArgs e)
        {
            Cancelled = false;
            if(AddingTask)
            {
                Item.SubItems.Add(taskBotBase.Text);
                Item.SubItems.Add(taskZone.Text);
                if (SwitchTime)
                {
                    Item.SubItems.Add(taskMinutes.Value.ToString());
                    Item.SubItems.Add("Minutes");
                }
                else
                {
                    Item.SubItems.Add(NumberOfNodes.Value.ToString());
                    Item.SubItems.Add("Nodes");
                }
                Logging.Write(string.Format("BuddyManager UI :: {0}", InternalSubList.Count));
                Item.SubItems.Add(InternalSubList.Count.ToString());
                string tmpStr = "";
                foreach (var str in InternalSubList)
                {
                    if (String.IsNullOrEmpty(tmpStr))
                        tmpStr = str;
                    else
                        tmpStr = string.Format("{0};{1}", tmpStr, str);
                }
                if (!String.IsNullOrEmpty(tmpStr))
                    Item.SubItems.Add(tmpStr);
                else
                    Item.SubItems.Add("None");
                this.Close();
                return;
            }
            Item.SubItems[1].Text = taskBotBase.Text;
            Item.SubItems[2].Text = taskZone.Text;
            if (SwitchTime)
            {
                Item.SubItems[3].Text = taskMinutes.Value.ToString();
                Item.SubItems[4].Text = "Minutes";
            }
            else
            {
                Item.SubItems[3].Text = NumberOfNodes.Value.ToString();
                Item.SubItems[4].Text = "Nodes";
            }
            Item.SubItems[5].Text = InternalSubList.Count.ToString();
            string tmpStr2 = "";
            foreach (var str in InternalSubList)
            {
                if (String.IsNullOrEmpty(tmpStr2))
                    tmpStr2 = str;
                else
                    tmpStr2 = string.Format("{0};{1}", tmpStr2, str);
            }
            if (!String.IsNullOrEmpty(tmpStr2))
                Item.SubItems[6].Text = tmpStr2;
            else
                Item.SubItems[6].Text = "None";
            this.Close();
        }

        private void SwitchAfterTimeRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (SwitchAfterTimeRadio.Checked)
            {
                taskMinutes.Enabled = true;
                label3.Enabled = true;
                label6.Enabled = true;

                label8.Enabled = false;
                NumberOfNodes.Enabled = false;

                SwitchTime = true;
            }
            else
            {
                taskMinutes.Enabled = false;
                label3.Enabled = false;
                label6.Enabled = false;

                label8.Enabled = true;
                NumberOfNodes.Enabled = true;

                SwitchTime = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAddSub_Click(object sender, EventArgs e)
        {
            var SP1 = new OpenFileDialog
            {
                Filter = "Xml files | *.xml",
                Title = "Select xml file to load!"
            };

            if (SP1.ShowDialog() == DialogResult.OK)
            {
                string profile1 = SP1.FileName;
                if (!string.IsNullOrEmpty(profile1))
                {
                    listViewSub.Items.Add(profile1);
                    InternalSubList.Add(profile1);
                }
            }
        }

        private void buttonDeleteSub_Click(object sender, EventArgs e)
        {
            try
            {
                var tmpList = InternalSubList.IndexOf(listViewSub.SelectedItems[0].Text);
                InternalSubList.RemoveAt(tmpList);
                listViewSub.Items.RemoveAt(listViewSub.SelectedItems[0].Index);
            }
            catch (Exception ex)
            { }
        }

    }
}
